<template>
  <div class="live">
    <!-- 位置显示 -->
    <div class='position'>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/live' }">入住登记</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 1. 搜索区域 -->
    <div class="header">
      <!-- change	仅在输入框失去焦点或用户按下回车时触发 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="宿舍名称">
          <el-input v-model="formInline.name" placeholder="请输入宿舍名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="findAccount()">查询</el-button>
          <el-button type="success" icon="el-icon-circle-plus-outline" @click="openAddDialog()">添加</el-button>
          <template>
            <download-excel class="export-excel-wrapper" :data="tableData" name="live.xls">
              <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
              <el-button type="primary" icon="el-icon-download">导出数据</el-button>
            </download-excel>
          </template>
        </el-form-item>
      </el-form>
    </div>

    <!-- 2. 表格区域展示视图数据 -->
    <div class="wrapper">
      <el-table :data="tableData" border>
        <el-table-column prop="lid" label="id" sortable width="70"></el-table-column>
        <el-table-column prop="student" label="入住学生" width="auto">
          <template slot-scope="scope">
            <span>{{scope.row.student.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="liveDate" label="入住时间" width="auto"></el-table-column>
        <el-table-column prop="dormitory" label="入住宿舍" width="100">
          <template slot-scope="scope">
            <span>{{scope.row.dormitory.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="auto"></el-table-column>
        <el-table-column prop="version" label="修改次数" width="auto"></el-table-column>
        <el-table-column prop="status" label="信息状态" width="100">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.status" effect="dark">正常</el-tag>
            <el-tag type="info" effect="dark" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="findDate(scope.row)">查看</el-button>
            <el-button type="primary" size="mini" v-if="scope.row.status==true" @click="handleEdit(scope.row)" icon="el-icon-edit">
              编辑</el-button>
            <el-button size="mini" type="danger" v-if="scope.row.status==true" @click="handleDelete(scope.row)" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 3. 分页 -->
    <MyPagination :total="total" :pageSize="pageSize" @SizeChange="SizeChange" @Pagechange="Pagechange" :currentPage="currentPage" />
    <!-- 查看数据 -->
    <el-dialog title="查看入住信息" :visible.sync="MessageDalog" width="70%" :before-close="handleClose">
      <el-table :data="rowData">
        <el-table-column prop="lid" label="id" sortable width="70"></el-table-column>
        <el-table-column prop="student" label="入住学生" width="auto">
          <template slot-scope="scope">
            <span>{{scope.row.student.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="liveDate" label="入住时间" width="auto"></el-table-column>
        <el-table-column prop="dormitory" label="入住宿舍" width="100">
          <template slot-scope="scope">
            <span>{{scope.row.dormitory.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="auto"></el-table-column>
        <el-table-column prop="version" label="修改次数" width="auto"></el-table-column>
        <el-table-column prop="status" label="信息状态" width="100">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.status" effect="dark">正常</el-tag>
            <el-tag type="info" effect="dark" v-else>停用</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加 -->
    <el-dialog title="添加管理员" :visible.sync="addDialog">
      <el-form :model="addfrom">
        <el-form-item label="未入住学生" label-width="120px">
          <el-select v-model="addfrom.studnetId" placeholder="请选择学生">
            <el-option v-for="studnet,index in studnetList" :key="index" :label="studnet.name" :value="studnet.sid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人数未满宿舍" label-width="120px">
          <el-select v-model="addfrom.dormitoryId" placeholder="请选择宿舍">
            <el-option v-for="dormitory,index in  dormitoryList" :key="index" :label="dormitory.name" :value="dormitory.did"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddAdmin()">取 消</el-button>
        <el-button type="primary" @click="submitAddFrom()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改dialog -->
    <el-dialog title="修改入住信息" :visible.sync="updateDialog">
      <el-form :model="updatefrom">
        <el-form-item label="学生" label-width="120px">
          <el-select v-model="updatefrom.studnetId" placeholder="请选择学生">
            <el-option v-for="studnet,index in oldstudnetList" :key="index" :label="studnet.name" :value="studnet.sid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍" label-width="120px">
          <el-select v-model="updatefrom.dormitoryId" placeholder="请选择宿舍">
            <el-option v-for="dormitory,index in  olddormitoryList" :key="index" :label="dormitory.name" :value="dormitory.did"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdateFrom()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { getolddormitoryList, getoldstudentList, getdormitoryList, getstudentList, GetPageAllLive, addLive, updateLive, deleteLive } from '@/api/api'
import MyPagination from "../components/MyPagination.vue";
export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      studnetList: [],
      dormitoryList: [],
      oldstudnetList: [],
      olddormitoryList: [],
      formInline: {
        name: "",
      },
      tableData: [],
      total: 5,
      pageSize: 5,//一页几条数据
      currentPage: 1, //选中的高亮页码
      //从这开始是查看数据的暂用变量
      MessageDalog: false,
      rowData: [],//行数据
      //从这开始是添加数据的暂用变量
      addDialog: false,
      addfrom: {
        studnetId: null,
        dormitoryId: null
      },
      //从这开始是修改
      updateDialog: false,
      oldData: {},//保存待修改的对象的值
      updatefrom: {
        studnetId: null,
        dormitoryId: null
      }
    }
  },
  mounted() {
    this.loadData()
    this.getStudentListInfo()
    this.getdormitoryListInfo()
    this.getAllStudentListInfo()
    this.getAlldormitoryListInfo()
  },
  methods: {
    getStudentListInfo() {
      getstudentList().then(res => {
        console.log(res);
        this.studnetList = res.data
      })
    },
    getdormitoryListInfo() {
      getdormitoryList().then(res => {
        console.log(res);
        this.dormitoryList = res.data
      })
    },
    getAllStudentListInfo() {
      getoldstudentList().then(res => {
        console.log(res);
        this.oldstudnetList = res.data
      })
    },
    getAlldormitoryListInfo() {
      getolddormitoryList().then(res => {
        console.log(res);
        this.olddormitoryList = res.data
      })
    },
    loadData() {
      GetPageAllLive({ pageSize: this.pageSize, pageNum: this.currentPage, name: this.formInline.name }).then((result) => {
        console.log(result);
        if (result.status == 200) {
          this.tableData = result.data.list
          this.total = result.data.total
          this.pageSize = result.data.pageSize
          this.currentPage = result.data.pageNum
        } else {
          this.$message.error(result.msg)
        }
      })
    },
    findAccount() {
      if (!this.formInline.name) return this.$message.error('输入数据为空无法查询')
      GetPageAllLive({ pageSize: this.pageSize, pageNum: this.currentPage, name: this.formInline.name }).then((result) => {
        console.log(result);
        if (result.status == 200) {
          this.$message.success(result.msg)
          this.tableData = result.data.list
          this.total = result.data.total
          this.pageSize = result.data.pageSize
          this.currentPage = result.data.pageNum
        } else {
          this.$message.error(result.msg)
        }
      })
      this.formInline.name = ''
    },
    Pagechange(pageNum) {//改变页码
      console.log(pageNum);
      this.currentPage = pageNum
      this.loadData()
    },
    SizeChange(pageSize) {//改变一页有几条
      console.log(pageSize);
      this.pageSize = pageSize
      this.loadData()
    },
    findDate(row) {//打开每行数据dialog
      console.log(row);
      this.rowData.push(row)//添加一行数据到rowdata变量
      this.MessageDalog = true//显示对话框
    },
    handleClose() {//关闭对话框
      this.MessageDalog = false
      this.rowData = []//清空数据
    },
    openAddDialog() {//打开新增dialog
      this.addDialog = true
    },
    cancelAddAdmin() {//关闭dialog 并且清空输入框
      this.$message.info('取消新增')
      this.addDialog = false
      this.addfrom.studnetId = null
      this.addfrom.dormitoryId = null
    },
    submitAddFrom() {
      if (this.addfrom.studnetId == null || this.addfrom.dormitoryId == null) return this.$message.error('请选择完整的信息')
      console.log(this.addfrom);
      this.handleAddLive()
    },
    handleAddLive() {//新增请求接口
      addLive({ sid: this.addfrom.studnetId, did: this.addfrom.dormitoryId }).then(res => {
        console.log(res);
        if (res.status == 200) {
          this.$message.success('添加成功')
          this.addfrom.studnetId = null
          this.addfrom.dormitoryId = null
          this.addDialog = false
          this.loadData()
        } else {
          return this.$message.error(res.msg)
        }
      })
    },
    handleEdit(data) {
      console.log(data);
      this.updateDialog = true
      this.updatefrom.studnetId = data.student.sid
      this.updatefrom.dormitoryId = data.dormitory.did
      this.oldData = data//保存就该瘴之前的数据
    },
    submitUpdateFrom() {//提交修改
      console.log(this.updatefrom);
      if (this.updatefrom.studnetId === this.oldData.student.sid && this.updatefrom.dormitoryId === this.oldData.dormitory.did) {
        return this.$message.error('与原来相同你修改个锤子')
      }
      //请求后端修改
      updateLive({ id: this.oldData.lid, sid: this.updatefrom.studnetId, did: this.updatefrom.dormitoryId }).then(res => {
        console.log(res);
        if (res.status == 200) {
          this.$message.success(res.msg)
          this.updateDialog = false
          this.loadData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleDelete(data) {//删除
      console.log(data);
      this.$confirm('确认删除此信息吗?')
        .then(_ => {
          deleteLive({ id: data.lid }).then(res => {
            console.log(res);
            if (res.status == 200) {
              this.$message.success(res.msg)
              this.loadData()
            } else {
              this.$message.error(res.msg)
            }
          })
        })
        .catch(_ => {
          this.$message.info('取消删除')
        });
    }
  }
}
</script>

<style lang='less' scoped>
.export-excel-wrapper {
  padding: 0;
  margin: 0;
  float: right;
}
.position {
  // background: #fff;
  padding: 10px;
  // border: 1px solid #eee;
  margin-bottom: 10px;
}
.header {
  display: flex;
  background: #fff;
  padding: 10px;
  border: 1px solid #eee;
  button {
    margin-left: 20px;
  }
  .el-form-item {
    margin-bottom: 0;
  }
}
.wrapper {
  margin: 10px 0;
}
</style>